package com.huiminpay.merchant.controller;

import com.aliyuncs.utils.StringUtils;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BizException;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王帅坤
 * @date 2021年06月11日14:29
 */
@Slf4j
@RestController
@Api(value = "支付渠道参数操作相关", tags = "支付渠道参数操作相关", description = "支付渠道参数操作相关1")
public class PayController {

    @Reference
    IPayService payService;

    @ApiOperation(value = "查询所有服务类型")
    @GetMapping("/my/platform-channels")
    public List<PlatformChannelDTO> findAll() {

        return payService.findAllPlatformChannel();
    }

    @ApiOperation(value = "开启服务")
    @PostMapping("/my/apps/{appId}/platform-channels")
    @ApiImplicitParam(name = "platformChannel", value = "服务类型编码", example = "huimin_c2b")
    public Integer openService(@PathVariable String appId, @RequestParam(required = true) String platformChannel) {

        //参数非空校验
        if (StringUtils.isEmpty(appId) || appId.equals("undefind")) {
            throw new BizException(CommonErrorCode.E_100007);
        }

        Integer integer = payService.openAppService(appId, platformChannel);
        return integer;
    }

    @ApiOperation(value = "查询服务是否开启")
    @GetMapping("/my/merchants/apps/platformchannels")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "platformChannel", value = "服务类型编码", example = "huimin_c2b"),
            @ApiImplicitParam(name = "appId", value = "应用ID", example = "189e0eefd02b474aa2b7d8bd7d84bfbb")
    })
    public Integer queryOpenService(@RequestParam String appId, @RequestParam(required = true) String platformChannel) {


        return payService.queryOpenService(appId, platformChannel);

    }

    @ApiOperation(value = "根据平台支付渠道（编码）获取原始支付渠道")
    @GetMapping("/my/pay-channels/platform-channel/{platformChannelCode}")
    @ApiImplicitParam(name = "platformChannelCode", value = "平台支付渠道（编码）", example = "huimin_b2c")
    public List<PayChannelDTO> findPlatformChannels(@PathVariable String platformChannelCode) throws BizException {

        //非空校验
        if (platformChannelCode == null) {
            throw new BizException(CommonErrorCode.E_200202);
        }
        //执行查询
        return payService.findPlatfromChannels(platformChannelCode);
    }

    @ApiOperation(value = "保存支付渠道参数")
    @PutMapping("/my/pay-channels")
    @ApiImplicitParam(name = "payChannelParamDTO", value = "平台支付渠道", dataType = "PayChannelParamDTO")
    public PayChannelParamDTO savePayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO) throws BizException {

        //非空校验

        //如果用vo类型需要进行转换

        //获取并注入merchantId
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);

        //保存或更新数据库
        return payService.savePayChannelParam(payChannelParamDTO);
    }

    @ApiOperation(value = "获取某平台应用下平台支付渠道所包含的原始支付参数列表")
    @GetMapping("my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", example = "0f0e8c2902014ff48158f82544e1e1db"),
            @ApiImplicitParam(name = "platformChannel", value = "平台支付渠道（编码）", example = "huimin_c2b")
    })
    public List<PayChannelParamDTO> queryPlatformChannelPayParams(@PathVariable String appId, @PathVariable String platformChannel) {
        //非空校验

        //数据查询

        return payService.queryPlatfromChannelPayParams(appId,platformChannel);
    }

    @ApiOperation(value = "获取某平台应用下平台支付渠道所包含的原始支付参数列表")
    @GetMapping("/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", example = "0f0e8c2902014ff48158f82544e1e1db"),
            @ApiImplicitParam(name = "platformChannel", value = "平台支付渠道（编码）", example = "huimin_c2b"),
            @ApiImplicitParam(name = "payChannel", value = "支付渠道编码", example = "ALIPAY_BAR_CODE")
    })
    public PayChannelParamDTO queryPlatformChannelPayParams(@PathVariable String appId,
                           @PathVariable String platformChannel, @PathVariable String payChannel) throws BizException {
        //非空校验

        //数据查询

        return payService.queryPlatformChannelPayParam(appId,platformChannel,payChannel);
    }
}

